package org.davidtrevien.com.imput.omi.panels.center.planning;

import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableColumn;

import org.davidtrevien.com.imput.datamodel.os.OS;
import org.davidtrevien.com.imput.datamodel.task.Task;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TOs;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TOsDaysModel;
import org.davidtrevien.com.imput.omi.panels.center.planning.model.TTask;
import org.davidtrevien.com.imput.omi.panels.center.planning.processdata.impl.TOsProcessData;
import org.davidtrevien.com.imput.omi.panels.center.planning.processdata.impl.TTaskProcessData;
import org.davidtrevien.com.imput.omi.panels.center.planning.renderer.DayTableCellRenderer;
import org.davidtrevien.utils.controler.Controller;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

public class PlanningCtrl
	extends Controller<PlanningView> {

	PlanningModel model;
	
	TOsProcessData tOsProcessData;
	
	TTaskProcessData tTaskProcessData;
	
	DayTableCellRenderer dayRenderer;

	public PlanningCtrl() {
		initComponent();
	}

	private void initComponent() {
		this.dayRenderer = new DayTableCellRenderer();
		this.view = new PlanningView(this.dayRenderer);
		this.model = new PlanningModel();
		
		this.tOsProcessData = new TOsProcessData();
		this.tTaskProcessData = new TTaskProcessData();

		int osId = 0;
		int taskId = 0;
		for (int i = 0; i < 2; i++) {
			//Create temporary model
			OS os = new OS(osId++);
			os.setOs("P-02820011");
			os.setTask("A1320");
			os.setName("HORIZON");
			
			Task task = new Task(taskId++, os);
			task.setNumberTCR("MOYEN_484");
			task.setLabel("Envoi des message CS_MULTI_update_characteristics, CS_MULTI_update_discrete_attributes, CS-MULTI_update_cst_kinematics systématique");
			
			this.model.addOS(os);
			this.model.addTask(task);
		}
		
		initListeners();

		//Mock
		this.model.setCurrentYear(2014);
		this.model.setCurrentMonth(0);
		
		/*new Thread(
			new Runnable() {
				@Override
				public void run() {
				}
			}
		).start();*/
		modelToView();
		
	} 
	
	private void initListeners() {
		this.view.tasktable.addTreeExpansionListener(new TreeExpansionListener() {
			
			@Override
			public void treeExpanded(TreeExpansionEvent event) {
				getView().daytable.expandRow(getView().tasktable.getRowForPath(event.getPath()));
			}
			
			@Override
			public void treeCollapsed(TreeExpansionEvent event) {
				getView().daytable.collapseRow(getView().tasktable.getRowForPath(event.getPath()));
			}
		});
		
		this.view.tasktable.addTreeSelectionListener(new TreeSelectionListener() {
			
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				int i = getView().tasktable.getRowForPath(e.getPath());
				getView().daytable.getSelectionModel().setSelectionInterval(i,i);
				
				Object selected = ((DefaultMutableTreeTableNode)e.getPath().getLastPathComponent()).getUserObject();
				if (selected instanceof TTask) {
					getView().taskDetailsCtrl.setTaskToDetail((TTask) selected);
				}
				else {
					getView().taskDetailsCtrl.setTaskToDetail(null);
				}
			}
		});
		this.view.daytable.addTreeSelectionListener(new TreeSelectionListener() {
			
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				int i = getView().daytable.getRowForPath(e.getPath());
				getView().tasktable.getSelectionModel().setSelectionInterval(i,i);
			}
		});
		
		((TOsDaysModel) this.view.daymodel).addTableEditionListener(new TableEditionListener() {
			
			@Override
			public void valueChanged(TableEditionEvent e) {
				TTask ttask = (TTask) e.getSource();
				//float previousConso = ttask.getConsomated();
				int cumul = 0;
				for (int i = 0; i < ttask.getMonthNbDays(); i++) {
					cumul+=ttask.getDay(i);
					//Update task model
					PlanningCtrl.this.model.getTask(ttask.getTos().getId(), ttask.getId())
						.setDay(i, 
								PlanningCtrl.this.model.getCurrentMonth(), 
								PlanningCtrl.this.model.getCurrentYear(), 
								ttask.getDay(i));
				}
				ttask.setConsomated(cumul);
				
				PlanningCtrl.this.view.tasktable.repaint();
				
			}
		});
		
		this.view.dateNavigationCtrl.add(new DateEditionListener() {
			
			@Override
			public void dateChanged(DateEditionEvent e) {
				int year = e.getYear();
				int month = e.getMonth();

				PlanningCtrl.this.model.setCurrentYear(year);
				PlanningCtrl.this.model.setCurrentMonth(month);
				modelToView();
			}
		});
	}
	
	public void modelToView() {
		final DefaultMutableTreeTableNode rootTask = this.view.rootTask;
		DefaultMutableTreeTableNode rootDay = this.view.rootDay;
		
		TOsDaysModel dayModel = (TOsDaysModel) this.view.daymodel;
		dayModel.setCurrentYear(this.model.getCurrentYear());
		
		for (TableColumn column : this.view.daytable.getColumns(true)) {
			column.setHeaderValue(this.view.daytable.getModel().getColumnName(column.getModelIndex()));
		}
		
		for(OS os : this.model.getOsList()) {
			TOs tos = new TOs(os.getId());
			this.tOsProcessData.proccessDataToT(os, tos);
			DefaultMutableTreeTableNode osNodeTask = new DefaultMutableTreeTableNode(tos);
			DefaultMutableTreeTableNode osNodeDay = new DefaultMutableTreeTableNode(tos);
			rootTask.add(osNodeTask);//Add os in task tree
			rootDay.add(osNodeDay);//Add os in day tree
			for(Task task : this.model.getTaskList(os.getId()).values()) {
				TTask ttask = new TTask(task.getId(), tos);
				this.tTaskProcessData.proccessDataToT(task, ttask);
				DefaultMutableTreeTableNode taskNodeTask = new DefaultMutableTreeTableNode(ttask);
				DefaultMutableTreeTableNode taskNodeDay = new DefaultMutableTreeTableNode(ttask);
				osNodeTask.add(taskNodeTask); //Add task in task tree
				osNodeDay.add(taskNodeDay); //Add task in day tree
			}
		}

		//this.view.daytable.updateUI();
		//this.view.tasktable.updateUI();
		
		//this.view.daytable.getTableHeader().getColumnModel().getColumn(28).setMinWidth(0);
		//this.view.daytable.getTableHeader().getColumnModel().getColumn(28).setPreferredWidth(0);

		//((DefaultTreeTableModel) this.view.taskmodel).setRoot(rootTask);
		//((DefaultTreeTableModel) this.view.daymodel).setRoot(rootDay);
		
		this.view.tasktable.expandAll();
		this.view.daytable.expandAll();

		this.dayRenderer.setWorkingMonth(this.model.getCurrentMonth());
		this.dayRenderer.setWorkingYear(this.model.getCurrentYear());
		
		this.view.daytable.repaint();
	}

	public PlanningModel getModel() {
		return this.model;
	}
	
}
